/**
 * a class to represent a solar system
 */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Master
 * @version $Revision: 1.0 $
 */
public class SolarSystem {

	/**
	 * Field SHI.
	 * (value is 10)
	 */
	private static final int SHI = 10;

	/**
	 * Field ERSHILIU.
	 * (value is 26)
	 */
	private static final int ERSHILIU = 26;

	/**
	 * Field SAN.
	 * (value is 3)
	 */
	private static final int SAN = 3;

	/**
	 * Field SISHI.
	 * (value is 40)
	 */
	private static final int SISHI = 40;

	/**
	 * Field YIQIAN.
	 * (value is 1000)
	 */
	private static final int YIQIAN = 1000;

	/**
	 * Field ERSHI.
	 * (value is 20)
	 */
	private static final int ERSHI = 20;

	/**
	 * Field YIWAN.
	 * (value is 10000)
	 */
	private static final int YIWAN = 10000;

	/**
	 * Field BASHIYI.
	 * (value is 81)
	 */
	private static final int BASHIYI = 81;

	/**
	 * Field ER.
	 * (value is 2)
	 */
	private static final int ER = 2;

	/**
	 * Field SHISAN.
	 * (value is 13)
	 */
	private static final int SHISAN = 13;

	/**
	 * Field LIU.
	 * (value is 6)
	 */
	private static final int LIU = 6;

	/**
	 * Field RESOURCENAME.
	 */
	public static final String RESOURCENAME[]={
		"NOSPECIALRESOURCES",
		"MINERALRICH",
		"MINERALPOOR",
		"DESERT",
		"LOTSOFWATER",
		"RICHSOIL",
		"POORSOIL",
		"RICHFAUNA",
		"LIFELESS",
		"WEIRDMUSHROOMS",
		"LOTSOFHERBS",
		"ARTISTIC",
		"WARLIKE"
	};

	/**
	 * Field name.
	 */
	private String name;
	
	/**
	 * Field techLevel.
	 */
	/**
	 * Field y.
	 */
	/**
	 * Field x.
	 */
	private int x, y, techLevel;

	/**
	 * Field planets.
	 */
	private List<Planet> planets;

	/**
	 * Field resources.
	 */
	private final int[] resources;

	/**
	 * Constructor for SolarSystem.
	 * @param name String
	 * @param x int
	 * @param y int
	 */
	public SolarSystem(String name, int x, int y){
		
		this.name=name;
		this.x=x;
		this.y=y;
		planets=new ArrayList<Planet>();
		techLevel=GameInfo.Rand.nextInt(LIU);
		resources=new int[SHISAN];
		for (int i=0;i < SHISAN;i++) resources[i]=GameInfo.Rand.nextInt(YIQIAN);
		final int num=ER + GameInfo.Rand.nextInt(SAN);
		for (int i=0;i < num;i++){
			int tx=this.x + GameInfo.Rand.nextInt(BASHIYI) - SISHI;
			int ty=this.y + GameInfo.Rand.nextInt(BASHIYI) - SISHI;
			while(GameInfo.Location.containsKey(tx * YIWAN + ty)){
				tx=this.x + GameInfo.Rand.nextInt(BASHIYI) - SISHI;
				ty=this.y + GameInfo.Rand.nextInt(BASHIYI) - SISHI;
			}
			planets.add(new Planet(this.generatePlanetName(), this, tx, ty));
			for (int j=-ERSHI;j < ERSHI;j++){
				for (int k=-ERSHI;k < ERSHI;k++){
					//System.out.println((tx+j)*10000+ty+k);
					GameInfo.Location.put((tx + j) * YIWAN + ty + k, 1);
				}
			}
		}
	}
	
	/**
	 * Method generatePlanetName.
	
	 * @return String */
	private String generatePlanetName(){

		String suffix="";
		for (int i=0;i < SAN;i++) suffix+=(char) (GameInfo.Rand.nextInt(ERSHILIU) + 'A');
		suffix+='-';
		for (int i=0;i < SAN;i++) suffix+=(char) (GameInfo.Rand.nextInt(SHI) + '0');
		return this.name + " " + suffix;

	}
	
	/**
	 * Method getName.
	
	 * @return String */
	public String getName() {
		return name;
	}

	/**
	 * Method getX.
	
	 * @return int */
	public int getX() {
		return x;
	}

	/**
	 * Method setX.
	 * @param x int
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * Method getY.
	
	 * @return int */
	public int getY() {
		return y;
	}

	/**
	 * Method setY.
	 * @param y int
	 */
	public void setY(int y) {
		this.y = y;
	}

	/**
	 * Method setName.
	 * @param name String
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Method getTechLevel.
	
	 * @return int */
	public int getTechLevel() {
		return techLevel;
	}

	/**
	 * Method setTechLevel.
	 * @param techLevel int
	 */
	public void setTechLevel(int techLevel) {
		this.techLevel = techLevel;
	}

	/**
	 * Method getPlanets.
	
	 * @return List<Planet> */
	public List<Planet> getPlanets() {
		return planets;
	}

	/**
	 * Method setPlanets.
	 * @param planets List<Planet>
	 */
	public void setPlanets(List<Planet> planets) {
		this.planets = planets;
	}

	/**
	 * Method setResource.
	 * @param name String
	 * @param value int
	 */
	public void setResource(String name, int value){
		for (int i=0;i < SHISAN;i++){
			
			if (name.equals(RESOURCENAME[i])){
				resources[i]=value;
			}
		}
		}
		
	/**
	 * Method getResource.
	 * @param name String
	
	 * @return int */
	public int getResource(String name){
		for (int i=0;i < SHISAN;i++){
			
			if (name.equals(RESOURCENAME[i])){
				return resources[i];
			}
		}
		return 0;
	}

	/**
	 * Method toString.
	
	 * @return String */
	@Override
	public String toString() {
		return "SolarSystem [name=" + name + ", x=" + x + ", y=" + y
				+ ", techLevel=" + techLevel + ", planets=" + planets
				+ ", resources=" + Arrays.toString(resources) + "]";
	}
	
	
}
